package tom.vertx.core.demo15;

import com.google.common.collect.Lists;
import io.vertx.core.cli.*;

import java.io.File;
import java.util.Arrays;

/**
 * vertx 提供了命令行工具，可以生成和解析命令行参数
 * @author ZHUFEIFEI
 */
public class CliDemo {

  public static void main(String[] args) {
    CLI cli = CLI.create("copy")
      .setSummary("A command line interface to copy files")
      //帮助,help=true
      .addOption(new Option()
        .setDescription("help for usage")
        .setLongName("help")
        .setShortName("h")
        .setFlag(true)
        .setHelp(true)
      )
      //添加一个可选配置标识，并设置  --全名，和  -短名
      .addOption(new Option()
        .setLongName("recursive")
        .setShortName("R")
        .setDescription("enable directory support")
        .setFlag(true)
      )
      //命令行参数参数
      .addArgument(new Argument()
        .setIndex(0)
        .setDescription("The source")
        .setArgName("source")
      )
      //使用Argument或Option时默认只接受string类型参数，对于确定类型可以指定类型参数使用TypedArgument
      .addArgument(new TypedArgument<File>()
        .setType(File.class)
        .setIndex(1)
        .setDescription("The target")
        .setArgName("target")
      )
      ;

    StringBuilder builder = new StringBuilder();
    //格式化Usage使用信息
    cli.usage(builder);
//    System.out.println(builder.toString());

    // CommandLine cmd = cli.parse(Lists.newArrayList(args));
    //help
    CommandLine cmd = cli.parse(Arrays.asList("-h"));
    System.out.println("cmd valid => " + cmd.isValid() + " flag enabled => " + cmd.isFlagEnabled("R"));
    // -h isValid=false
    if (!cmd.isValid() && cmd.isAskingForHelp()) {
      builder.setLength(0);
      cli.usage(builder);
      System.out.println(builder.toString());
    }

    //解析命令行参数
    cmd = cli.parse(Arrays.asList("-R", "D:/log/abc", "D:/log/abc_cp"));
    System.out.println("cmd valid => " + cmd.isValid() + " flag enabled => " + cmd.isFlagEnabled("R"));

    System.out.println("option value => " + cmd.getOptionValue("R"));
    System.out.println("arg 0 => " + cmd.getArgumentValue(0));
    File f = cmd.getArgumentValue(1);
    System.out.println("arg 1 => " + f.getAbsolutePath());



  }

}
